var ddgl_app = function() {
	var path = Constants.epUrlPath;
	var ractive;
    var param_ddzt = "-1";
    var param_ddlb = "-1";
    var param_ywy = "000000";
	function renderView() {

        
		ractive = new Ractive({
			el : ".main-contexts",
			template : "#main-template",
			onrender: function() {
				//manager.loadProperties(this, "ddgl");
				//manager.loadProperties(this, "error");
			},
			oncomplete : function() {
				var ywy_ = getQueryString("ywy");
				if(ywy_){
					param_ywy = ywy_;
				}
				var _sc_param = {
						pageSize : Constants.page.pagesize,
						currPage : 1,
						ywy : param_ywy
				};
				initTable(_sc_param,false);
				
				/*var quanbu = "全部";
	            $.get(manager.getContextPath() + "common/getCodeListByCodeClass?codeClass=DHLB", function (data) {
	                //增加全部的提示
	                data.unshift({"code": -1, "codedesc": quanbu});
	                store.set("DDLB", JSON.stringify(data));
	                selecttextInfo();
	                setWDCode("ddgl_DDLB", -1);
	            });
	            
                changeTab();*/
			}
		});

		ractive.on({
			search: function () {
		       /* $(".changeTab").removeClass("active");
		        $(".firstTab").addClass("active");
		        param_ddzt = "-1";*/
                loadtable();
            },
			pldc : function() {
				pldc();
			}
		});

        /*function changeTab() {
            //切换TAB
            $(".changeTab").bind("click", function () {
                $(this).addClass("active");
                $(this).siblings().removeClass("active");
                param_ddzt = $(this).attr("id");
                //ractive.set("ddzt",param_ddzt);
                loadtable();
            });
        }*/
	}

	function review() {
		var _param = {};
		if (typeof ractive != "undefined") {
			var param = ractive.get("param_ddgl");
			param.lpmc = $.trim(param.lpmc);
			param.jfqs = $.trim(param.jfqs);
			param.jfjs = $.trim(param.jfjs);
			param.dhrmc = $.trim(param.dhrmc);
			_param.lpmc = param.lpmc;
			_param.jfqs = param.jfqs;
			_param.jfjs = param.jfjs;
			_param.dhrmc = param.dhrmc;
			_param.dhsjqs = param.dhsjqs ? (param.dhsjqs.length == 10 ? param.dhsjqs + "T00:00:00+08" : param.dhsjqs) : "";
			_param.dhsjjs = param.dhsjjs ? (param.dhsjjs.length == 10 ? param.dhsjjs + "T23:59:59+08" : param.dhsjjs) : "";
			_param.ddlb = param_ddlb;
			_param.zt = param_ddzt;
			_param.ywy = param_ywy;
			ractive.set("param_ddgl", param);
		}
		return _param;
	}

	function initTable(param,isSearch) {
		var _title_id = "订单编号";
		var _title_dhsj = "交易时间";
		var _title_ddlb = "订单类别";
		var _title_lpmc = "名称";
		var _title_jf = "价格（积分）";
		var _title_dhrmc = "用户昵称";
		/*var _title_zt = "订单状态";*/
		//var _title_cz = ractive.get("i18n_cpsz_cz");
		
		var _table_title = [_title_id, _title_dhsj ,_title_ddlb, _title_lpmc, _title_jf, _title_dhrmc];
		var _col_model = [
				{
					name : 'id',
					width : 60
				},
				{
					name : 'dhsj',
					width : 100,
			        innerhtml: function (opts) {
			            if (opts.dhsj) {
			                return moment(opts.dhsj).format("YYYY-MM-DD HH:mm:ss");
			            } else {
			                return "";
			            }
			        }
				},
				{
					name : 'ddlbDesc',
					width : 40
				},
				{
					name : 'lpmc',
					width : 100,
					float:'left'
				},
				{
					name : 'jf',
					width : 60
				},
				{
					name : 'dhrmc',
					width : 80,
					float:'left'
				}/*,
				{
					name : 'zt',
					width : 80,
					innerhtml:function(opts){
						var str = opts.ztDesc;
						if(opts.zt == 1){
							str = "<font class='ad-font-orange'>" + opts.ztDesc + "</font>"
						}
						return str;
					}
				}*/
				];
		jQuery("#wd-table").wdGrid({
			url:path+'ddgl/getDdcxPagination',
			colNames : _table_title,
			colModel : _col_model,
			dataType : "GET",
			submitType : "noForm",
			postData : param,
			pager : "#wd-pager",
	        checkflg: true,
	        checktext:["id"],
			rowList: Constants.page.rowList,
	        pagesize: Constants.page.pagesize
		});
	}
    
    //加载table
    function loadtable(){
    	//重新加载table
		var _cx_param = {
				pageSize : Constants.page.pagesize,
				currPage : 1
		};
		var _param = review();
		// 合并查询参数
		var objs = $.extend(_cx_param, _param);
		initTable(objs,true);
		
    }
    
    function pldc(){
    	var ddids = $("#wd-table").getAllCheckedId();
		if(!ddids){
			alert("请勾选需要导出的订单！");
			return false;
		}
		window.open(path + "ddgl/pldcDdxx?ddids=" + ddids + "&ywy=" + param_ywy);
    }
	
	/* 获取地址栏传递的参数 */
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
    
	return {
		init : function() {
			renderView();
		},
		pldc : function() {
			pldc();
		}
	};
}();
$(document).ready(ddgl_app.init);